﻿using System;
using System.Collections.Generic;
using System.Text;
using Microserve.TeleEye.Common.MenuBar;
using Microserve.TeleEye.Data;
using System.Xml;
using System.Web;
using System.Web.UI;

namespace Microserve.TeleEye.BusinessServices
{
    public class MenuController
    {
        private List<MenuItem> mainMenu = new List<MenuItem>();
        public MenuController()
        {
            MenuItem home = new MenuItem() { Title = "Home", Type=MenuType.Top, NavigateUrl="default.aspx" };
            mainMenu.Add(home);

            MenuItem corporate = new MenuItem() { Title = "About Us", Type = MenuType.Top, NavigateUrl = "product.aspx?s=aboutus" };
            corporate.SubMenu.Add(new MenuItem() { Title = "About Us", Type = MenuType.Top, NavigateUrl = "product.aspx?s=aboutus" });
            corporate.SubMenu.Add(new MenuItem() { Title = "Our Technology", Type = MenuType.Top, NavigateUrl = "section.aspx?s=technologyhome" });
            corporate.SubMenu.Add(new MenuItem() { Title = "Awards", Type = MenuType.Top, NavigateUrl = "product.aspx?s=awards" });
            corporate.SubMenu.Add(new MenuItem() { Title = "Press Center", Type = MenuType.Top, NavigateUrl = "product.aspx?s=presscenter" });
            corporate.SubMenu.Add(new MenuItem() { Title = "e-News", Type = MenuType.Top, NavigateUrl = "product.aspx?s=enews" });
            corporate.SubMenu.Add(new MenuItem() { Title = "CEO Corner", Type = MenuType.Top, NavigateUrl = "product.aspx?s=ceocorner" });
            mainMenu.Add(corporate);

            MenuItem products = new MenuItem() { Title = "Products", Type= MenuType.Top, NavigateUrl="section.aspx?s=productshome" };
            products.SubMenu.Add(new MenuItem() { Title = "Products Home", Type = MenuType.Top, NavigateUrl="section.aspx?s=productshome" });
            products.SubMenu.Add(new MenuItem() { Title = "Video Recording Servers", Type = MenuType.Top, NavigateUrl = "productgroup.aspx?p=videorecordingserver" });            
            products.SubMenu.Add(new MenuItem() { Title = "HD CCTV Cameras", Type = MenuType.Top, NavigateUrl="productgroup.aspx?p=hdcctvcamera" });            
            products.SubMenu.Add(new MenuItem() { Title = "Network Cameras / Video Servers", Type = MenuType.Top, NavigateUrl = "productgroup.aspx?p=networkcameras" });
            products.SubMenu.Add(new MenuItem() { Title = "Standalone DVRs", Type = MenuType.Top, NavigateUrl="productgroup.aspx?p=standalonedvr" });                        
            products.SubMenu.Add(new MenuItem() { Title = "Network Video Monitoring Software", Type = MenuType.Top, NavigateUrl="productgroup.aspx?p=networkvideomonitoring"});
            products.SubMenu.Add(new MenuItem() { Title = "Speed Domes / Positioning Cameras", Type = MenuType.Top, NavigateUrl="productgroup.aspx?p=speeddomes" });
            products.SubMenu.Add(new MenuItem() { Title = "CCTV / Pinhole Cameras / CCTV Lens", Type = MenuType.Top, NavigateUrl = "productgroup.aspx?p=cctv" });
            products.SubMenu.Add(new MenuItem() { Title = "Fixed Domes", Type = MenuType.Top, NavigateUrl = "productgroup.aspx?p=fixeddomes" });
            products.SubMenu.Add(new MenuItem() { Title = "IR Cameras / Domes", Type = MenuType.Top, NavigateUrl="productgroup.aspx?p=ircameras" });
            products.SubMenu.Add(new MenuItem() { Title = "Accessoriess", Type = MenuType.Top, NavigateUrl = "productgroup.aspx?p=accessories" });
            mainMenu.Add(products);         

            MenuItem applications = new MenuItem() { Title = "Applications", Type = MenuType.Top, NavigateUrl = "section.aspx?s=applicationhome" };
            applications.SubMenu.Add(new MenuItem() { Title = "Applications Home", Type = MenuType.Top, NavigateUrl="section.aspx?s=applicationhome" });
            applications.SubMenu.Add(new MenuItem() { Title = "Retail Management", Type = MenuType.Top, NavigateUrl="product.aspx?s=retailmanagement" });
            applications.SubMenu.Add(new MenuItem() { Title = "Banking & ATM Surveillance", Type = MenuType.Top, NavigateUrl = "product.aspx?s=banking" });
            applications.SubMenu.Add(new MenuItem() { Title = "Property Management", Type = MenuType.Top, NavigateUrl = "product.aspx?s=propertymanagement" });
            applications.SubMenu.Add(new MenuItem() { Title = "Alarm Centre", Type = MenuType.Top, NavigateUrl = "product.aspx?s=alarmcenter" });
            applications.SubMenu.Add(new MenuItem() { Title = "Construction Site Monitoring", Type = MenuType.Top, NavigateUrl = "product.aspx?s=constructionsite" });
            applications.SubMenu.Add(new MenuItem() { Title = "Logistics Managemente", Type = MenuType.Top, NavigateUrl = "product.aspx?s=logistics" });
            applications.SubMenu.Add(new MenuItem() { Title = "Public Utilities Monitoring", Type = MenuType.Top, NavigateUrl = "product.aspx?s=publicutilities" });
            applications.SubMenu.Add(new MenuItem() { Title = "e-Promotion", Type = MenuType.Top, NavigateUrl = "product.aspx?s=epromo" });
            applications.SubMenu.Add(new MenuItem() { Title = "Video on Data Services", Type = MenuType.Top, NavigateUrl = "product.aspx?s=videoondata" });
            mainMenu.Add(applications);

            MenuItem support = new MenuItem() { Title = "Support", Type = MenuType.Top, NavigateUrl = "section.aspx?s=supporthome" };
            //support.SubMenu.Add(new MenuItem() { Title = "Support Home", Type = MenuType.Top, NavigateUrl="section.aspx?s=supporthome" });
            //support.SubMenu.Add(new MenuItem() { Title = "Downloads", Type = MenuType.Top });
            //support.SubMenu.Add(new MenuItem() { Title = "Product FAQs", Type = MenuType.Top, NavigateUrl="product.aspx?s=faq"});
            //support.SubMenu.Add(new MenuItem() { Title = "System Configuration", Type = MenuType.Top });
            //support.SubMenu.Add(new MenuItem() { Title = "Warranties", Type = MenuType.Top });
            //support.SubMenu.Add(new MenuItem() { Title = "<i>sure</i>LINK Registration", Type = MenuType.Top });
            //support.SubMenu.Add(new MenuItem() { Title = "Software Registration", Type = MenuType.Top });
            //support.SubMenu.Add(new MenuItem() { Title = "Email Support", Type = MenuType.Top });
            //support.SubMenu.Add(new MenuItem() { Title = "Feedback", Type = MenuType.Top });
            //support.SubMenu.Add(new MenuItem() { Title = "Customer Information Update", Type = MenuType.Top });
            //mainMenu.Add(support);

            MenuItem whereToBuy = new MenuItem() { Title = "Where to Buy", Type = MenuType.Top, NavigateUrl="section.aspx?s=wheretobuyhome" };
            //whereToBuy.SubMenu.Add(new MenuItem() { Title = "Where to Buy Home", Type = MenuType.Top });
            //whereToBuy.SubMenu.Add(new MenuItem() { Title = "<i>Tele</i>Eye Offices", Type = MenuType.Top });
            //whereToBuy.SubMenu.Add(new MenuItem() { Title = "<i>Tele</i>Eye Distributors", Type = MenuType.Top });
            mainMenu.Add(whereToBuy);

            MenuItem events = new MenuItem() { Title = "Live Demo", Type = MenuType.Top, NavigateUrl="demoregistration.aspx" };
            //events.SubMenu.Add(new MenuItem() { Title = "Events Home", Type = MenuType.Top });
            //events.SubMenu.Add(new MenuItem() { Title = "Exhibitions", Type = MenuType.Top, NavigateUrl = "product.aspx?s=exhibitions" });
            //events.SubMenu.Add(new MenuItem() { Title = "Seminars &amp; Trainings", Type = MenuType.Top });
            mainMenu.Add(events);

            MenuItem technology = new MenuItem() { Title = "Careers", Type = MenuType.Top, NavigateUrl = "section.aspx?s=career" };
            //technology.SubMenu.Add(new MenuItem() { Title = "Technology Home", Type = MenuType.Top, NavigateUrl = "section.aspx?s=technologyhome" });
            //technology.SubMenu.Add(new MenuItem() { Title = "SMAC-M", Type = MenuType.Top, NavigateUrl = "product.aspx?s=smacm" });
            //technology.SubMenu.Add(new MenuItem() { Title = "<i>sure</i>LINK", Type = MenuType.Top, NavigateUrl = "product.aspx?s=surelink" });
            //technology.SubMenu.Add(new MenuItem() { Title = "BS 8418", Type = MenuType.Top, NavigateUrl = "product.aspx?s=bs8418" });
            //technology.SubMenu.Add(new MenuItem() { Title = "Network CCTV", Type = MenuType.Top, NavigateUrl = "product.aspx?s=networkcctv" });
            mainMenu.Add(technology);


            MenuItem contact = new MenuItem() { Title = "Contact Us", Type = MenuType.Top, NavigateUrl = "contactus.aspx" };
            mainMenu.Add(contact);
        }

        public List<MenuItem> GetMainMenu()
        {
            return mainMenu;
        }

        public List<MenuItem> GetSectionMenu(HttpContext context, string sectionName)
        {
            List<MenuItem> sectionMenu = new List<MenuItem>();

            string path = context.Request.MapPath("static/xml/" + sectionName + ".xml");

            SectionHome sectionHome = new SectionHome();
            XmlDocument doc = sectionHome.GetSideMenu(path);

            XmlNodeList sideMenus = doc.SelectNodes("section/sidemenus/sidemenu");

            foreach (XmlNode item in sideMenus)
            {
                XmlNodeList subMenus = item.SelectNodes("item");
                
                MenuItem mItem = new MenuItem()
                {
                    Title = item.Attributes["title"].Value
                };

                foreach (XmlNode subItem in subMenus)
                {
                    mItem.SubMenu.Add(new MenuItem()
                    {
                        Title = subItem.Attributes["title"].Value,
                        NavigateUrl = subItem.Attributes["url"].Value
                    });
                }

                sectionMenu.Add(mItem);
            }

            return sectionMenu;
        }

        public bool ShowCommonMenu(HttpContext context, string sectionName)
        {
            bool showCommonMenu = true;

            string path = context.Request.MapPath("static/xml/" + sectionName + ".xml");

            SectionHome sectionHome = new SectionHome();
            XmlDocument doc = sectionHome.GetSideMenu(path);

            XmlNode sideMenus = doc.SelectSingleNode("section/sidemenus");

            if (sideMenus != null)
            {
                if (sideMenus.Attributes["showCommonMenu"] is XmlAttribute)
                {
                    string show = sideMenus.Attributes["showCommonMenu"].Value;
                    bool.TryParse(show, out showCommonMenu);
                }
            }

            return showCommonMenu;
        }

        public List<MenuItem> GetPageMenu(string pageName, bool fromMainSite)
        {
            List<MenuItem> pageMenus = new List<MenuItem>();

            MenuItem menu = new MenuItem();
            menu.Type = MenuType.Sidebar;
            menu.Title = "What's new?";

            HomePage homePage = new HomePage();

            if (fromMainSite)
            {
                XmlDocument whatsNew = homePage.GetPageMenu("What's New");
                XmlDocument upcomingEvents = homePage.GetPageMenu("Upcoming Events");
                XmlNodeList whatsNewItems = whatsNew.SelectNodes("div/a");
                XmlNodeList upcomingEventsItems = upcomingEvents.SelectNodes("div/a");

                foreach (XmlNode item in whatsNewItems)
                {
                    menu.SubMenu.Add(new MenuItem()
                    {
                        Title = item.InnerText,
                        Target = item.Attributes["href"].Value,
                        Type = MenuType.Sidebar
                    });
                }

                MenuItem menu2 = new MenuItem();
                menu2.Type = MenuType.Sidebar;
                menu2.Title = "Upcoming Events";

                foreach (XmlNode item in upcomingEventsItems)
                {
                    menu2.SubMenu.Add(new MenuItem()
                    {
                        Title = item.InnerText,
                        Target = item.Attributes["href"].Value,
                        Type = MenuType.Sidebar
                    });
                }

                pageMenus.Add(menu);
                pageMenus.Add(menu2);
            }
            else
            {
                MenuController controller = new MenuController();
                pageMenus = controller.GetSectionMenu(HttpContext.Current,
                            pageName);
            }

            return pageMenus;
        }
    }
}
